<template>
  <div class="container">
    <div class="app-container">
      <!-- 展示树形结构 -->
      <el-tree :expand-on-click-node="false" default-expand-all :data="depts" :props="defaultProps">
        <!-- 123 -->
        <!-- data 解构depts数据 -->
        <template v-slot="{ data }">
          <!-- 节点结构 -->
          <el-row style="width: 100%; height: 40px;" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manger">{{ data.manageName }}</span>
              <el-dropdown @command="handleCommand($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 放置弹层 -->
    <!-- sync 表示接受子组件的事件，update:showDialog -->
    <!-- @updateDepartment 父组件监听updateDepartment事件,刷新列表 -->
    <add-dept ref="addDept" :current-node-id="currentNodeId" :show-dialog.sync="showDialog" @updateDepartment="getDepartment" />
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/listTree'
import addDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    addDept
  },
  data() {
    return {
      // 演示数据
      // depts: [
      //   {
      //     name: '传智教育',
      //     manageName: '管理员',
      //     children: [
      //       {
      //         name: '人事部',
      //         manageName: '张三'
      //       },
      //       {
      //         name: '行政部',
      //         manageName: '李四'
      //       },
      //       {
      //         name: '技术部',
      //         manageName: '王五'
      //       }
      //     ]
      //   }
      // ],
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 控制弹层显示隐藏
      currentNodeId: null // 存储当前点击的id
    }
  },
  created() {
    // 调用获取数据接口
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const res = await getDepartment()
      this.depts = transListToTreeData(res, 0)
    },
    handleCommand(type, id) {
      if (type === 'add') {
        this.showDialog = true
        this.currentNodeId = id
      } else if (type === 'edit') {
        this.showDialog = true
        this.currentNodeId = id

        /**
         * 调用子组件的方法，getDepartmentDetail
         *
         * 这里为什么要用$nextTick，因为弹出层之后紧接着要调用子组件方法，第一次弹出层时，
         * 里面的组件还未完成渲染，所以要等到showDialog这个属性设置的数据完成渲染之后才进行调用
         */
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        // 删除部门
        this.$confirm('此操作将永久删除该部门, 是否继续?').then(async() => {
          await delDepartment(id)
          // 提示消息
          this.$message.success('删除部门成功')
          this.getDepartment()
        })
      }
    }

  }
}
</script>
<style scoped>
.app-container {
  padding: 20px 140px;
  font-size: 14px;
}
.tree-manger {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
